using System.Data;
using System.Data.SqlClient;
using Provausio.Common.Data;
using Provausio.Common.Objects;
using Provausio.Common.Utility.Extensions;

namespace TrainingManagement.api.Models.Data.CRUD.Location
{
    public class LocationInsertDataSource : NonQuerySource<BusinessObjects.Location>
    {
        public override bool Execute(BusinessObjects.Location request)
        {
            const string insertLocation = "dbo.training_api_LocationInsert";
            var parameters = new SqlParameter[2];
            parameters[0] = new SqlParameter("@Title", SqlDbType.NVarChar){Value = request.Title};
            parameters[1] = new SqlParameter("@LocationID", SqlDbType.Int){Direction = ParameterDirection.Output};

                var result = SqlHelper.ExecuteNonQuery(
                    ConnectionStrings.TrainingDb,
                    CommandType.StoredProcedure,
                    insertLocation,
                    parameters) != 0;
                request.LocationId = parameters[1].Value.DbTo<int>();
                return result;
        }
    }
}